/*
 * TarifaBuilderPaso1.java
 *
 * Created on 4 de junio de 2007, 11:13 AM
 */

package inc.jmc.cyberboard.ui.ventas;

import inc.jmc.cyberboard.Tarifa;
import inc.jmc.cyberboard.common.DefaultLookAndFeel;
import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import inc.jmc.cyberboard.common.NumFormat;
import inc.jmc.cyberboard.dao.HistoricoVentasDao;

/**
 *
 * @author  JOlguin01
 */
public class TarifaBuilderPaso4 extends javax.swing.JPanel {
    
    /** Creates new form TarifaBuilderPaso1 */
	TarifaBuilderFrm parent;
	static Logger logger = Logger.getLogger(TarifaBuilderPaso4.class.getName());
    /**
     * @param parent
     */
    public TarifaBuilderPaso4(TarifaBuilderFrm parent) {
        initComponents();
        this.nTxt.setBackground(DefaultLookAndFeel.TableBackground);
        this.precioTxt.setBackground(DefaultLookAndFeel.TableBackground);
        this.parent=parent;
    }

    /**
     * @return
     */
    public javax.swing.JTextField getPrecioHoraTxt() {
        return precioHoraTxt;
    }

    public javax.swing.JTextField getNTxt() {
        return nTxt;
    }

    public javax.swing.JTextField getPrecioTxt() {
        return precioTxt;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nTxt = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        precioTxt = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        precioHoraTxt = new javax.swing.JTextField();
        ecuacionButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setLayout(null);

        jPanel1.setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Paso 4");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 10, 110, 20);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("<html>Define el precio de la hora completa</html>");
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        jPanel1.add(jLabel2);
        jLabel2.setBounds(50, 30, 130, 50);

        nTxt.setEditable(false);
        nTxt.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPanel1.add(nTxt);
        nTxt.setBounds(10, 140, 60, 19);

        jLabel3.setText("<html>Fracciones de hora</html>");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(10, 90, 60, 40);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel4.setText("X");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(75, 140, 10, 17);

        precioTxt.setEditable(false);
        precioTxt.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPanel1.add(precioTxt);
        precioTxt.setBounds(90, 140, 60, 19);

        jLabel6.setText("<html>Precio por fraccion de hora</html>");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(90, 90, 60, 40);

        jLabel7.setText("<html>Precio hora completa</html>");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(190, 90, 60, 40);

        precioHoraTxt.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPanel1.add(precioHoraTxt);
        precioHoraTxt.setBounds(180, 140, 60, 19);

        ecuacionButton.setText("=");
        ecuacionButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        ecuacionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ecuacionButtonActionPerformed(evt);
            }
        });

        jPanel1.add(ecuacionButton);
        ecuacionButton.setBounds(160, 140, 20, 19);

        add(jPanel1);
        jPanel1.setBounds(10, 20, 260, 210);

        nextButton.setText(">>");
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        add(nextButton);
        nextButton.setBounds(220, 240, 49, 23);

        backButton.setText("<<");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        add(backButton);
        backButton.setBounds(120, 240, 49, 23);

        cancelButton.setText("Cancelar");
        cancelButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        add(cancelButton);
        cancelButton.setBounds(20, 240, 73, 23);

    }// </editor-fold>//GEN-END:initComponents

    private void ecuacionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ecuacionButtonActionPerformed

        String preciotxt = this.precioTxt.getText();
        String ntxt = this.nTxt.getText();
    	
        if(preciotxt!=null && !preciotxt.equals("") && ntxt!=null && !ntxt.equals(""))
        {
        	try {

            float preciotxt1=Float.parseFloat(preciotxt);
            float ntxt1=Float.parseFloat(ntxt);
            parent.paso4.getPrecioTxt().setText(NumFormat.nf_comas_00.format(parent.tarifaAux.getPrecioFraccionHora()));
            float precioaux1 = preciotxt1 * ntxt1;
            this.precioHoraTxt.setText(NumFormat.nf_00.format(precioaux1));
            	} catch (NumberFormatException  ex) {
				JOptionPane.showMessageDialog(this,"Introduzca un valor numerico-"+ex.getMessage(),"Verifique los datos proporcionados",JOptionPane.ERROR_MESSAGE);
				
			}
        }
    }//GEN-LAST:event_ecuacionButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed

        parent.tarifaAux=new Tarifa();
        parent.getContentPane().remove(parent.paso4);
        parent.getContentPane().add(parent.getJPanel1(), java.awt.BorderLayout.CENTER);
        parent.updateUI();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed

        parent.getContentPane().remove(parent.paso4);
        parent.getContentPane().add(parent.paso3, java.awt.BorderLayout.CENTER);
        parent.updateUI();
    }//GEN-LAST:event_backButtonActionPerformed

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed

    	String text = this.precioHoraTxt.getText();
    	
        if(text!=null && !text.equals(""))
        {
        	try{
            float precio1=Float.parseFloat(text);
            parent.tarifaAux.setPrecioHora(precio1);
            parent.getContentPane().remove(parent.paso4);
            if(parent.getAddRadioButton().isSelected())
            	parent.paso5.setTarifaIntervalos(parent.tarifaAux.getFraccionesHora(), 
            			                         new Float(parent.tarifaAux.getPrecioFraccionHora())
            	                                 );
            if(parent.getModifyRadioButton().isSelected())
            {
            	String aux1=this.nTxt.getText();
            	int npartes=Integer.parseInt(aux1);
            	if(parent.tarifaAux.getPreciosEnTabla()!=null && parent.tarifaAux.getPreciosEnTabla().length!=npartes)
            	{
            		parent.paso5.setTarifaIntervalos(parent.tarifaAux.getFraccionesHora(), 
	                         new Float(parent.tarifaAux.getPrecioFraccionHora())
                            );
            	}
            	else
            		parent.paso5.setTarifaIntervalos(parent.tarifaAux.getPreciosEnTabla());
            	
            }
            	
            
            
            parent.getContentPane().add(parent.paso5, java.awt.BorderLayout.CENTER);
            parent.updateUI();
            logger.info("Hasta aqui la tarifa que se esta configurando es esta:"+parent.tarifaAux);
        	} catch (NumberFormatException ex) {
				JOptionPane.showMessageDialog(this,"Introduzca un valor numerico-"+ex.getMessage(),"Verifique los datos proporcionados",JOptionPane.ERROR_MESSAGE);
				
			}
            
        }
    }//GEN-LAST:event_nextButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton ecuacionButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField nTxt;
    private javax.swing.JButton nextButton;
    private javax.swing.JTextField precioHoraTxt;
    private javax.swing.JTextField precioTxt;
    // End of variables declaration//GEN-END:variables
    
}
